﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using InTheHand.Net.Bluetooth;

namespace BlueRadar.API
{
    public class BlueSupport
    {
        private BluetoothRadio br;
        public bool IsBluetoothSupported { get; set; }
        public RadioMode RadioMode { get; set; }
        public bool IsDiscoverable { get; set; }

        public string LocalAddress { get; set; }
        public string Manufacturer { get; set; }
        public string Mode { get; set; }
        public string Name { get; set; }
        public string SoftwareManufacturer { get; set; }

        public BlueSupport()
        {
            br = BluetoothRadio.PrimaryRadio;
            this.Manufacturer = br.Manufacturer.ToString();
            this.LocalAddress = br.LocalAddress.ToString();
            this.Mode = br.Mode.ToString();

            this.Name = br.Name;    
            this.SoftwareManufacturer = br.SoftwareManufacturer.ToString();

            SetSettings();
        }

        public void ChangeDeviceMode(string mode)
        {
            switch(mode)
            {
                case "Discoverable":
                    this.br.Mode = RadioMode.Discoverable;
                    break;
                case "PowerOff":
                    this.br.Mode = RadioMode.PowerOff;
                    break;
                default:
                    this.br.Mode = RadioMode.Connectable;
                    break;
            }
        }

        private void SetSettings()
        {
            if (br != null)
            {
                this.IsBluetoothSupported = true;
                this.RadioMode = br.Mode;
                if (br.Mode == RadioMode.Discoverable)
                    this.IsDiscoverable = true;
            }
        }

        public void MakeDiscoverable()
        {
            if(br.Mode != RadioMode.Discoverable)
                br.Mode = RadioMode.Discoverable;
        }
    }
}
